﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HunterBase : MonoBehaviour
{

    Rigidbody2D m_rd;  
    bool m_FacingRight;
    //bool m_Attacking;
    bool m_canMove;

    //JUMP
    [SerializeField] private LayerMask m_GroundLayerMasks;
    [SerializeField] private LayerMask m_BlockLayer;
    [SerializeField] private Transform m_GroundCheck;
    [Range(0f, .5f)] [SerializeField] private float m_GroundCheckRadius = 0.1f;
    [SerializeField] float jumpForce = 300f;
    bool m_jumping;
    bool m_ground;
    bool m_blocked;
    bool m_isUpStair;

    //跳跃射线
    [Range(0f, 1f)] [SerializeField] private float m_jumpDis = 0.5f;

    HunterAnimController anim;

    private void Awake()
    {
        //m_Attacking = false;
        m_FacingRight = false;
        m_canMove = true;
        m_jumping = false;
        m_ground = true;
        m_blocked = false;

        m_rd = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<HunterAnimController>();
    }

    /// <summary>
    /// 行走
    /// </summary>
    /// <param name="target"></param>
    public void Walk(Vector2 target,float speed)
    {

        Flip(target);
        if (m_canMove)
        {
            //m_rd.velocity = movement * m_moveSpeed;
            //m_rd.MovePosition(m_rd.position + movement * Time.fixedDeltaTime);
            transform.position = Vector2.MoveTowards(transform.position,new Vector2(target.x,transform.position.y) , speed * Time.fixedDeltaTime); 
            anim.PlayWalk(speed);
            m_ground = false;
            CheckJump();   
        }      
    }



    /// <summary>
    /// 猎人攻击基类
    /// </summary>
    public virtual void Attack(Transform Player)
    {

        anim.PlayAttack();

        StopMove();
    }

    public void Idle()
    {
        m_rd.velocity = Vector2.zero;
        anim.PlayWalk(0);
    }

    /// <summary>
    /// 停止移动
    /// </summary>
    public void StopMove()
    {
        m_canMove = false;
    }

    public void StartMove()
    {
        m_canMove = true;
    }



    /// <summary>
    /// 猎人跳跃
    /// </summary>
    public void Jump()
    {
        if (m_ground)
        {
            m_jumping = false;
            anim.PlayJump(m_jumping);
        }
         
        if (m_isUpStair && m_blocked)
        {
            m_jumping = true;
            m_rd.AddForce(new Vector2(0, jumpForce / 15));
            anim.PlayWalk(1);
        }
        else if (m_ground && m_blocked)
        {

            m_jumping = true;
            m_rd.AddForce(new Vector2(0, jumpForce));
            anim.PlayJump(m_jumping);
        }
    }

    /// <summary>
    /// 猎人射线检测
    /// </summary>
    public void CheckJump()
    {
        //RaycastHit2D hit;

        //if(Physics2D.Raycast(transform.position,transform.forward,jumpDis,jumpLayer,))

        Ray2D ray = new Ray2D(new Vector2(transform.position.x,transform.position.y+0.2f), new Vector2(-transform.localScale.x,0));
        RaycastHit2D rayInfo = Physics2D.Raycast(ray.origin, ray.direction,m_jumpDis,m_BlockLayer);

        m_ground = Physics2D.OverlapCircle(m_GroundCheck.position, m_GroundCheckRadius, m_GroundLayerMasks);

        if (rayInfo.collider != null)
        {
            if (rayInfo.collider.tag == "Platform" && m_isUpStair == false)
                m_blocked = false;
            else
                m_blocked = true;           
        }    
        Jump();
        m_blocked = false;       
    }

    /// <summary>
    /// 转头
    /// </summary>
    /// <param name="movement"></param>
    public void Flip(Vector2 target)
    {
        Vector3 scale = transform.localScale;
        float movement = target.x - transform.position.x;
        if (movement > 0 && !m_FacingRight || movement < 0 && m_FacingRight)
        {
            StopMove();
            scale.x *= -1;
            transform.localScale = scale;

            m_FacingRight = !m_FacingRight;
            anim.PlayTurn();
            Invoke("StartMove", 0.8f);
        }
        
    }

    public void CheckUpStair(Vector2 target)
    {
        float distance = target.y - transform.position.y;
        //Debug.Log(distance);
        if (distance >= 1f)
        {
            m_isUpStair = true;
            gameObject.layer = LayerMask.NameToLayer("Default");

        }
        else
        {
            m_isUpStair = false;
            gameObject.layer = LayerMask.NameToLayer("Hunter");
        }
    }
}
